package com.ply.flashsalessystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.entity.pojo.Manage;
import com.ply.flashsalessystem.mapper.ManageMapper;
import com.ply.flashsalessystem.service.ManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@Service
public class ManageServiceImpl extends ServiceImpl<ManageMapper, Manage> implements ManageService {
    @Autowired
    ManageMapper manageMapper;

    @Override
    public void addAdmin(String name, String pwd) {
        Manage manageByName = getManageByName(name);
        if (manageByName == null){
            // 和用户 注册逻辑类似
            //todo 敏感词 还有 正则 配合

            // 可以注册
            Manage manage = new Manage();
            manage.setName(name);
            manage.setPwd(pwd);
            manageMapper.insert(manage);
        }
    }

    @Override
    public void deleteManage(Integer manageId) {
        manageMapper.removeById(manageId);
    }

    /**
     * 通过 管理员id 精准查询
     */
    @Override
    public Manage getManageOne(Integer current, Integer limit, Integer manageId) {
        return manageMapper.selectById(manageId);
    }

    /**
     * 通过 name 精准查询
     */
    @Override
    public Manage getManageByName(String name) {
        QueryWrapper<Manage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return manageMapper.selectOne(queryWrapper);
    }

    /**
     * 查询 所有 管理员
     */
    @Override
    public Page<Manage> getManageAll(Integer current, Integer limit) {
        return manageMapper.selectPage(new Page<>(current, limit), null);
    }

    /**
     * 通过 name 模糊查询
     * @param name name
     */
    @Override
    public Page<Manage> getManageByLikeName(Integer current, Integer limit, String name) {
        QueryWrapper<Manage> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        return manageMapper.selectPage(new Page<>(current, limit), queryWrapper);
    }


}
